package com.fzz.learning_forum.controller;

import com.fzz.learning_forum.domain.Article;
import com.fzz.learning_forum.domain.User;
import com.fzz.learning_forum.service.ArticleService;
import com.fzz.learning_forum.service.UserService;
import com.fzz.learning_forum.serviceImpl.ArticleServiceImpl;
import com.fzz.learning_forum.serviceImpl.UserServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService=new UserServiceImpl();

    @Autowired
    private ArticleService articleService=new ArticleServiceImpl();

    @RequestMapping(value = "/user/")
    public String index(HttpServletRequest request)
    {
        return this.index(request, 1, 5);
    }

    // 分页查询文章
    @RequestMapping(value = "/page/{p}")
    public String index(HttpServletRequest request, @PathVariable("p") int page, @RequestParam(value = "count", defaultValue = "5") int count) {
        PageInfo<Article> articleList = articleService.selectArticleWithPage(page, count);
        HttpSession session=request.getSession();
        session.setAttribute("articleList", articleList);
        return "client/index";
    }

    // 根据id查询文章
    @RequestMapping(value = "/userArticles/{p}/{id}")
    public String userArticles(HttpServletRequest request,@PathVariable("id") Integer id, @PathVariable(value = "p") int page, @RequestParam(value = "count", defaultValue = "5") int count) {
        System.out.println(id);
        PageInfo<Article> articleList = articleService.selectArticleWithPageById(page, count,id);
        HttpSession session=request.getSession();
        session.setAttribute("articles", articleList);
        return "client/myDynamicState";
    }

    //修改用户信息
    @RequestMapping(value = "/user/editUserInfo")
    @ResponseBody
    public String editUserInfo(HttpSession session, User user)
    {
        Integer rows = userService.editUserInfo(user);
        if(rows>0)
        {
            session.setAttribute("user",user);
            return "OK";
        }
        return "NO";
    }

    //根据tags模糊查询文章
    @RequestMapping(value = "/user/selectArticleByTags")
    public String selectArticleByTags(HttpServletRequest request, @RequestParam(value = "p",defaultValue = "1") int page, @RequestParam(value = "count", defaultValue = "5") int count, String tags)
    {
        System.out.println(tags);
        PageInfo<Article> articleList = articleService.selectArticleByTags(page,count,tags);
        System.out.println(articleList);
        HttpSession session=request.getSession();
        session.setAttribute("articleList", articleList);
        return "client/index";
    }


    @RequestMapping(value = "user/publishArticl")
    public String toPublish()
    {
        return "back/article_edit";
    }

}
